package com.chb.controller;

import com.chb.annotation.LogAnnotation;
import com.chb.model.entity.*;
import com.chb.model.vo.BranchGoodsVo;
import com.chb.model.vo.Result;
import com.chb.service.BranchService;
import com.chb.service.FileService;
import com.chb.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author ：njh
 * @date ：Created in 2023/9/27 15:43
 * @description：商品信息模块controller
 * @modified By：
 * @version:
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private BranchService branchService;

    @Autowired
    private FileService fileService;


    /**
     * @param currentPage,pageSize
     * @return Result
     * @create by: njh
     * @description:查询所有商品信息
     * @create time: 2023/9/27 15:44
     */
    @GetMapping("/allGoods")
    @ResponseBody
    public Result allGoods(Integer currentPage, Integer pageSize, Integer staffBranchid) {

        Result result = goodsService.findAllGoods(currentPage, pageSize, staffBranchid);

        return result;
    }


    /**
     * @param goods
     * @return
     * @create by: njh
     * @description: 修改商品信息
     * @create time: 2023/10/9 11:16
     */
    @RequestMapping("/updGoods")
    @ResponseBody
    @LogAnnotation(business_type = "修改商品信息")
    public void updGoods(@RequestBody Goods goods) {

        goodsService.updGoods(goods);

    }

    /**
     * @param goods
     * @return
     * @create by: njh
     * @description: 添加商品
     * @create time: 2023/9/27 16:54
     */
    @RequestMapping("/addGoods")
    @ResponseBody
    @LogAnnotation(business_type = "添加商品信息")
    public void addGoods(@RequestBody Goods goods) {

        goodsService.addGoods(goods);

    }


    /**
     * @param id 商品id
     * @return
     * @create by: njh
     * @description: 根据id删除分店的商品信息
     * @create time: 2023/10/9 11:16
     */
    @RequestMapping("/delBranchGoods")
    @ResponseBody
    @LogAnnotation(business_type = "删除商品信息")
    public void delBranchGoods(Integer id) {

        goodsService.delBranchGoods(id);

    }

    /**
     * @param page     当前页数
     * @param pageSize 每页展示最大数
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 获取添加商品所需的全部商品数据
     * @create time: 2023/9/27 20:11
     */
    @GetMapping("/goodsList")
    @ResponseBody
    public Result goodsList(Integer page, Integer pageSize) {
        return goodsService.goodsList(page, pageSize);
    }


    /**
     * @param file
     * @return
     * @create by: njh
     * @description: 图片上传
     * @create time: 2023/10/8 9:50
     */
    @PostMapping("/imgup")
    @ResponseBody
    public Result imgUp(@RequestParam("file") MultipartFile file) throws IOException {

        String pictureUrl = fileService.upload(file);
        return Result.success(pictureUrl);
    }


    /**
     * @param pictureUrl 图片url
     * @return Result
     * @create by: njh
     * @description: 图片删除
     * @create time: 2023/10/8 11:07
     */
    @PostMapping("/imgdelete")
    @ResponseBody
    public Result imgUrl(@RequestParam("identificationPhoto") String pictureUrl) {
        fileService.removeFile(pictureUrl);
        return Result.success();
    }

    /**
     * @param
     * @return Result
     * @create by: njh
     * @description: 查找下拉框要显示的所有的键帽类型
     * @create time: 2023/10/8 12:24
     */
    @PostMapping("/searchKeyCap")
    @ResponseBody
    public Result searchKeyCap() {

        List<KeyCap> keyCapName = goodsService.searchKeyCap();

        return Result.success(keyCapName);
    }

    /**
     * @param
     * @return Result
     * @create by: njh
     * @description: 查找下拉框要显示的所有的键盘颜色
     * @create time: 2023/10/8 12:24
     */
    @PostMapping("/searchKeyBoardColor")
    @ResponseBody
    public Result searchKeyBoardColor() {

        Result result = goodsService.searchKeyBoardColor(null, null, null, null);

        System.out.println(result);

        return result;
    }

    /**
     * @param
     * @return Result
     * @create by: njh
     * @description: 查找下拉框要显示的所有的轴体选择
     * @create time: 2023/10/8 12:24
     */
    @PostMapping("/searchShaftChoose")
    @ResponseBody
    public Result searchShaftChoose() {

        Result result = goodsService.searchShaftChoose(null, null, null, null);

        return result;
    }

    /**
     * @param
     * @return Result
     * @create by: njh
     * @description: 查找下拉框要显示的所有的背光颜色
     * @create time: 2023/10/8 12:24
     */
    @PostMapping("/searchBackLightColor")
    @ResponseBody
    public Result searchBackLightColor() {

        Result result = goodsService.searchBackLightColor(null, null, null, null);

        return result;
    }

    /**
     * @param
     * @return Result
     * @create by: njh
     * @description: 查找下拉框要显示的所有的分店名
     * @create time: 2023/10/8 12:24
     */
    @PostMapping("/searchBranch")
    @ResponseBody
    public Result searchBranch() {

        Result result = branchService.findAllBranch();

        return result;
    }


    /**
     * @param currentPage
     * @param pageSize
     * @param goodsname                商品名称
     * @param searchKeyCapList         搜索的键帽类型
     * @param searchKeyBoardColorList  搜索的键盘颜色
     * @param searchShaftChooseList    搜索的轴体类型
     * @param searchBackLightColorList 搜索的背光颜色
     * @param searchBranchList         搜索的分店id
     * @return Result
     * @create by: njh
     * @description: 根据条件查询商品
     * @create time: 2023/10/8 17:29
     */
    @RequestMapping("searchGoods")
    @ResponseBody
    public Result searchGoods(Integer currentPage, Integer pageSize, String goodsname, Integer searchKeyCapList, Integer searchKeyBoardColorList, Integer searchShaftChooseList, Integer searchBackLightColorList, Integer searchBranchList) {

        Result result = goodsService.searchGoods(currentPage, pageSize, goodsname, searchKeyCapList, searchKeyBoardColorList, searchShaftChooseList, searchBackLightColorList, searchBranchList);

        return result;
    }

}